package com.tepth.remote.control.manager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.tepth.remote.control.base.bean.Project;
import com.tepth.remote.control.base.config.SpKey;
import com.tepth.remote.core.utils.storage.ControlPreference;

import java.util.ArrayList;

/**
 * Description:
 *
 * @author Hequn.Lee
 * @date 2018/11/9
 */
public class ConfigManager {

    private static final class Holder {
        private static final ConfigManager INSTANCE = new ConfigManager();
    }

    public static ConfigManager getInstance() {
        return Holder.INSTANCE;
    }

    public ConfigManager setRealName(String realName) {
        ControlPreference.addCustomAppProfile(SpKey.KEY_REAL_NAME, realName);
        return this;
    }

    public ConfigManager setPhoneId(String phoneId) {
        ControlPreference.addCustomAppProfile(SpKey.KEY_PHONE_ID, phoneId);
        return this;
    }

    public ConfigManager setUid(int uid) {
        ControlPreference.addCustomAppProfile(SpKey.KEY_INTEGER_UID, uid);
        return this;
    }

    public ConfigManager setIsZj(String isZj) {
        ControlPreference.addCustomAppProfile(SpKey.KEY_BOOLEAN_ZJ, isZj);
        return this;
    }

    public ConfigManager setIsBm(String isBm) {
        ControlPreference.addCustomAppProfile(SpKey.KEY_BOOLEAN_BM, isBm);
        return this;
    }

    public ConfigManager setProjects(ArrayList<Project> projects) {
        ControlPreference.addCustomAppProfile(SpKey.KEY_OBJECT_PROJECTS, projects);
        return this;
    }

    public String getRealName() {
        return ControlPreference.getPreferenceString(SpKey.KEY_REAL_NAME);
    }

    public String getPhoneId() {
        return ControlPreference.getPreferenceString(SpKey.KEY_PHONE_ID);
    }

    public int getUid() {
        return ControlPreference.getPreferenceInteger(SpKey.KEY_INTEGER_UID);
    }

    public String getIsZj() {
        return ControlPreference.getPreferenceString(SpKey.KEY_BOOLEAN_ZJ);
    }

    public String getIsBm() {
        return ControlPreference.getPreferenceString(SpKey.KEY_BOOLEAN_BM);
    }

    public ArrayList<Project> getProjects() {
        final JSONArray array = ControlPreference.getPreferenceJSONArray(SpKey.KEY_OBJECT_PROJECTS);
        final ArrayList<Project> results = new ArrayList<>();
        for (int i = 0; i < array.size(); i++) {
            results.add(JSON.parseObject(JSON.toJSONString(array.getJSONObject(i)), new TypeReference<Project>() {
            }));
        }
        return results;
    }
}
